package com.liang.controller;


import com.github.pagehelper.PageInfo;
import com.liang.Event.Events.NoticePublishEvent;
import com.liang.Event.Events.receiveNoticeEvent;
import com.liang.Event.PublishService;
import com.liang.pojo.dto.NoticeDto;
import com.liang.pojo.Notice;
import com.liang.empty.Result;
import com.liang.pojo.userandnotice;
import com.liang.service.NoticeService;
import com.liang.service.UserandnoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.List;
/**
 *@作者:lsk
 *Controller接口
 *@Date:2023/10/16
*/
@RestController
@RequestMapping("/NoticeURL")
public class NoticeController {


	@Autowired
	private NoticeService noticeservice;

	@Autowired
	PublishService publishService;

	@Autowired
	UserandnoticeService userandnoticeService;



	/**
	根据Noticeid查询
	*/
	@GetMapping("/selectByNoticeid/{noticeid}")
	public Result selectByNoticeid(@PathVariable(value = "noticeid") Long noticeid){
		return Result.buildSuccess(noticeservice.selectByNoticeid(noticeid));
	}


	/**
	根据Noticeid更新
	*/
	@PostMapping("/updateByNoticeid")
	public Result updateByNoticeid(@RequestBody Notice demo){
		return Result.buildSuccess(noticeservice.updateByNoticeid( demo));
	}



	/**
	根据Noticeid删除
	*/
	@GetMapping("/deleteByNoticeid/{noticeid}")
	public Result deleteByNoticeid(@PathVariable(value = "noticeid") Long noticeid){
		return Result.buildSuccess(noticeservice.deleteByNoticeid(noticeid));
	}

	/**
	 insertOrUpdate(插入或更新(根据主键或唯一索引更新，主键和唯一索引不会改变))
	*/
	@PostMapping("/insertOrUpdate")
	public Result insertOrUpdate(@RequestBody Notice demo){
		return Result.buildSuccess(noticeservice.insertOrUpdate(demo));
	}



	/**
	 insertBath:(批量插入)
	*/
	@PostMapping("/insertBatch")
	public Result insertBatch(@RequestBody List<Notice> list){
		return Result.buildSuccess(noticeservice.insertBatch(list));
	}



	/**
	 insertOrUpdateBatch:(批量插入或更新)
	*/
	@PostMapping("/insertOrUpdateBatch")
	public Result insertOrUpdateBatch(@RequestBody List<Notice> list){
		return Result.buildSuccess(noticeservice.insertOrUpdateBatch(list));
	}



	/**
	 selectList:(根据参数查询集合)
	*/
	@PostMapping("/selectList")
	public Result selectList(@RequestBody Map<String,Object> param){

		return Result.buildSuccess(noticeservice.selectList(param),noticeservice.selectCount(param));
	}



	/**
	 selectCount:(根据集合查询数量)
	*/
	@PostMapping("/selectCount")
	public Result selectCount(@RequestBody Map<String,Object> param){
		return Result.buildSuccess(noticeservice.selectCount(param));
	}

	/**
	 * 自己写的代码
	 */


	/**
	 * 师部给下属发送通知公告发布通知公告或者教育学习
	 */
	@PostMapping("/intsert")
	@Transactional(rollbackFor = Exception.class)
	public Result  NoticePublish(@RequestBody Notice notice){

		//存储这个notice信息到数据库中,返回主键id回填到notice实体类中
		Boolean bit = noticeservice.NoticePublish(notice);

		//存储需要接收notice信息的人员id到redis中，并且添加到userandnotice中。
		publishService.pub(new NoticePublishEvent(notice.getId(),notice.getNoticeid()));

		if(bit)  return Result.buildMessage(null,"发布成功",0);
		else return  Result.buildFail(0,"服务器繁忙");
	}


	/**
	 * 用户接收通告学习
	 */
	@PostMapping("/receiveNotice")
	@Transactional(rollbackFor = Exception.class)
	public Result receiveNotice(@RequestBody userandnotice userandnotice){

		//设置notice_status的值
		userandnotice.setNotice_status(true);
		Boolean bit = userandnoticeService.setNoticeStatus(userandnotice);

		//判断是否成功,成功则发布事件删除redis中存储的用户id
		if(bit){
			//发布时间，处理redis中的用户id信息。
			publishService.pub(new receiveNoticeEvent(userandnotice));
			//返回结果结合
			return  Result.buildMessage(null,"确认成功",0);
		}
		else return Result.buildFail(1,"确认失败");
	}

	/**
	 * 分页查找本人接收的通知公告
	 */

	@PostMapping("/getNoticesAndPage")
	public Result getNoticesAndPage(@RequestBody NoticeDto noticeDto){

		PageInfo<NoticeDto> pageInfo = noticeservice.getNoticesAndPage(noticeDto);

		return Result.buildSuccess(pageInfo);
	}

	/**
	 * 查看本人发布历史     //应该修改了查看本单位发布历史
	 * @param noticeDto
	 * @return
	 */
	@PostMapping("/getNoticesHistory")
	public Result getNoticesHistory(@RequestBody NoticeDto noticeDto){

		System.out.println(noticeDto.toString());
		PageInfo<NoticeDto> pageInfo = noticeservice.getNoticesHistory(noticeDto);

		return Result.buildSuccess(pageInfo);
	}

	//对未接受消息的用户发送通知
	@GetMapping("/publishNotice/{noticeId}")
	public Result publishNotice(@PathVariable("noticeId") Long noticeId){
		Boolean bit =	noticeservice.publishNotice(noticeId);
	 	if(bit) return Result.buildSuccess();
	 	else return Result.buildFail(1,"服务器繁忙!");
	}

	/**
	 * 查看上级通知
	 * @param noticeDto
	 * @return
	 */
	@PostMapping("/superiorNotice")
	public  Result superiorNotice(@RequestBody NoticeDto noticeDto){

		PageInfo<NoticeDto> pageInfo = noticeservice.getSuperiorNotice(noticeDto);
		return Result.buildSuccess(pageInfo);
	}


}